//
//  MyNetworkTools.m
//  SinaWeibo
//
//  Created by Apple on 16/8/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "MyNetworkTools.h"
#import "MySQLiteTools.h"

@implementation MyNetworkTools

+(instancetype)shareManger{
    
    static MyNetworkTools *tools;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [[MyNetworkTools alloc]initWithBaseURL:nil sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration ]];
        
        tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
    });
    return tools;
    
}

-(void)getUserinfoSuccess:(successUser)success failure:(failure)failure{
    
    
    
    AcountModel *acountModel = [AcountModel acountModelFromSandbox];
    
    NSDictionary *parameters = @{
                                 @"access_token":acountModel.access_token,
                                 @"uid":acountModel.uid
                                 };
    
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    
    [self GET:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        UserModel *user = [UserModel mj_objectWithKeyValues:responseObject];
        
        success(user);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failure(error);
        
    }];
    
    
}

-(void)getStatuesSuccess:(successStatues)success failure:(failure)failure{
    
    //从本地获取当前access_token的本地数据，如果有，就直接加载本地的数据
    NSArray *nativeStatues = [[MySQLiteTools shareSqliteTools] getNativeData];
    
    if (nativeStatues.count > 0) {
        success(nativeStatues);
    }else{
        //2.封装数据
        AcountModel *acountModel = [AcountModel acountModelFromSandbox];
        
        NSDictionary *parameters = @{
                                     @"access_token":acountModel.access_token,
                                     };
        
        NSString *stringURL = @"https://api.weibo.com/2/statuses/friends_timeline.json";
        
        //3.发送请求
        [self GET:stringURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
            //3.1获取数据成功后，保存数据
            
            NSArray *statuesModel = [StatuesModel mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
            
            success(statuesModel);
            
            //3.2获取微博数组成功后存储到本地
            for (NSDictionary *dict in responseObject[@"statuses"]) {
                
                if ([[MySQLiteTools shareSqliteTools] insertWithDict:dict]) {
                    
                }else{
                    DDLogDebug(@"插入失败");
                }
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"首页获取失败%@",error);
        }];
    }
}

-(void)getNewStatuesWithSince_id:(NSString *)since_id Success:(successStatues)success failure:(failure)failure{

    //1.封装请求数据
    AcountModel *acountModel = [AcountModel acountModelFromSandbox];
    
    NSDictionary *parameters = @{
                                 @"access_token":acountModel.access_token,
                                 @"since_id":since_id
                                 };
    NSString *stringURL = @"https://api.weibo.com/2/statuses/friends_timeline.json";
    //2.发送请求
    [self GET:stringURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //1.将字典数组转化为模型数组
        NSArray *modelArray = [StatuesModel mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
        
        success(modelArray);
        
        //更新插入数据数据库
        //3.2先删除原有的表,获取微博数组成功后存储到本地
        
        for (NSDictionary *dict in responseObject[@"statuses"]) {
            
            if ([[MySQLiteTools shareSqliteTools] insertWithDict:dict]) {
                
                NSLog(@"更新成功");
                
            }else{
                DDLogDebug(@"插入失败");
            }
        }
    
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {

    }];
    
}

-(void)getNewStatuesWithMax_id:(NSString *)max_id Success:(successStatues)success failure:(failure)failure{

    
    //2.封装请求数据
    
    AcountModel *acountModel = [AcountModel acountModelFromSandbox];
    
    
    NSDictionary *parameters = @{
                                 @"access_token":acountModel.access_token,
                                 @"max_id":max_id
                                 };
    NSString *stringURL = @"https://api.weibo.com/2/statuses/friends_timeline.json";
    //3.发送请求
    [self GET:stringURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //1.将字典数组转化为模型数组
        NSArray *modelArray = [StatuesModel mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
        
        success(modelArray);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failure(error);
        
    }];

    
}

-(void)getUnreadSuccess:(successBadge)success failure:(failure)failure{
    
    
    AcountModel *acount = [AcountModel acountModelFromSandbox];
    
    
    NSDictionary *parameters = @{
                                 @"access_token":acount.access_token,
                                 @"uid":acount.uid
                                 };
    NSString *stingURl = @"https://rm.api.weibo.com/2/remind/unread_count.json";
    
    [self GET:stingURl parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"%@---unread",responseObject);
        
        NSNumber *badge = responseObject[@"status"];
        
        success(badge.stringValue);
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failure(error);
    }];
    
}



@end
